{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import geopandas as gpd\n",
    "import h5py\n",
    "import glob\n",
    "import os\n",
    "import matplotlib.pyplot as plt\n",
    "from bokeh.plotting import figure, output_file, show, save\n",
    "from bokeh.models import ColumnDataSource, HoverTool,LinearColorMapper\n",
    "\n",
    "import numpy as np\n",
    "from ipyleaflet import Map, basemaps, basemap_to_tiles, GeoData, LayersControl\n",
    "import geopandas\n",
    "import json"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "hdf5_flist = glob.glob('./Outputs/*.h5')\n",
    "f = h5py.File(hdf5_flist[0], 'r')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['./Outputs/processed_ATL06_20181231145919_00480211_001_01.h5']"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "hdf5_flist"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [],
   "source": [
    "f.keys()\n",
    "\n",
    "\"\"\"create empty beam specific dataframes\"\"\"\n",
    "gtl1_df = pd.DataFrame(columns = ['delta_time','lat','lon','h_li','x_atc', 'atl06_quality_summary', 'bsnow_conf'])\n",
    "gtr1_df = pd.DataFrame(columns = ['delta_time','lat','lon','h_li','x_atc', 'atl06_quality_summary', 'bsnow_conf'])\n",
    "gtl2_df = pd.DataFrame(columns = ['delta_time','lat','lon','h_li','x_atc', 'atl06_quality_summary', 'bsnow_conf'])\n",
    "gtr2_df = pd.DataFrame(columns = ['delta_time','lat','lon','h_li','x_atc', 'atl06_quality_summary', 'bsnow_conf'])\n",
    "gtl3_df = pd.DataFrame(columns = ['delta_time','lat','lon','h_li','x_atc', 'atl06_quality_summary', 'bsnow_conf'])\n",
    "gtr3_df = pd.DataFrame(columns = ['delta_time','lat','lon','h_li','x_atc', 'atl06_quality_summary', 'bsnow_conf'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [],
   "source": [
    "gt1l = f['gt1l']\n",
    "gt1r = f['gt1r']\n",
    "gt2l = f['gt2l']\n",
    "gt2r = f['gt2r']\n",
    "gt3l = f['gt3l']\n",
    "gt3r = f['gt3r']\n",
    "iceseg_gtl1 = gt1l['land_ice_segments']\n",
    "iceseg_gt1r = gt1r['land_ice_segments']\n",
    "iceseg_gt21 = gt2l['land_ice_segments']\n",
    "iceseg_gt2r = gt2r['land_ice_segments']\n",
    "iceseg_gt31 = gt3l['land_ice_segments']\n",
    "iceseg_gt3r = gt3r['land_ice_segments']\n",
    "\n",
    "\n",
    "\n",
    "\"\"\"adding beam specific time to df's\"\"\"\n",
    "gtl1_df['delta_time'] = iceseg_gtl1['delta_time']\n",
    "gtr1_df['delta_time'] = iceseg_gt1r['delta_time']\n",
    "gtl2_df['delta_time'] = iceseg_gt21['delta_time']\n",
    "gtr2_df['delta_time'] = iceseg_gt2r['delta_time']\n",
    "gtl3_df['delta_time'] = iceseg_gt31['delta_time']\n",
    "gtr3_df['delta_time'] = iceseg_gt3r['delta_time']\n",
    "\n",
    "\n",
    "\"\"\"adding beam specific lat to df's\"\"\"\n",
    "gtl1_df['lat'] = iceseg_gtl1['latitude']\n",
    "gtr1_df['lat'] = iceseg_gt1r['latitude']\n",
    "gtl2_df['lat'] = iceseg_gt21['latitude']\n",
    "gtr2_df['lat'] = iceseg_gt2r['latitude']\n",
    "gtl3_df['lat'] = iceseg_gt31['latitude']\n",
    "gtr3_df['lat'] = iceseg_gt3r['latitude']\n",
    "\n",
    "\"\"\"adding beam specific lon to df's\"\"\"\n",
    "gtl1_df['lon'] = iceseg_gtl1['longitude']\n",
    "gtr1_df['lon'] = iceseg_gt1r['longitude']\n",
    "gtl2_df['lon'] = iceseg_gt21['longitude']\n",
    "gtr2_df['lon'] = iceseg_gt2r['longitude']\n",
    "gtl3_df['lon'] = iceseg_gt31['longitude']\n",
    "gtr3_df['lon'] = iceseg_gt3r['longitude']\n",
    "\n",
    "\"\"\"adding beam specific h_li to df's\"\"\"\n",
    "gtl1_df['h_li'] = iceseg_gtl1['h_li']\n",
    "gtr1_df['h_li'] = iceseg_gt1r['h_li']\n",
    "gtl2_df['h_li'] = iceseg_gt21['h_li']\n",
    "gtr2_df['h_li'] = iceseg_gt2r['h_li']\n",
    "gtl3_df['h_li'] = iceseg_gt31['h_li']\n",
    "gtr3_df['h_li'] = iceseg_gt3r['h_li']\n",
    "\n",
    "\"\"\"adding beam specific x_atc to df's\"\"\"\n",
    "gtl1_df['x_atc'] = iceseg_gtl1['ground_track']['x_atc']\n",
    "gtr1_df['x_atc'] = iceseg_gt1r['ground_track']['x_atc']\n",
    "gtl2_df['x_atc'] = iceseg_gt21['ground_track']['x_atc']\n",
    "gtr2_df['x_atc'] = iceseg_gt2r['ground_track']['x_atc']\n",
    "gtl3_df['x_atc'] = iceseg_gt31['ground_track']['x_atc']\n",
    "gtr3_df['x_atc'] = iceseg_gt3r['ground_track']['x_atc']\n",
    "\n",
    "\"\"\"adding quality flag to df's\"\"\"\n",
    "gtl1_df['atl06_quality_summary'] = iceseg_gtl1['atl06_quality_summary']\n",
    "gtr1_df['atl06_quality_summary'] = iceseg_gt1r['atl06_quality_summary']\n",
    "gtl2_df['atl06_quality_summary'] = iceseg_gt21['atl06_quality_summary']\n",
    "gtr2_df['atl06_quality_summary'] = iceseg_gt2r['atl06_quality_summary']\n",
    "gtl3_df['atl06_quality_summary'] = iceseg_gt31['atl06_quality_summary']\n",
    "gtr3_df['atl06_quality_summary'] = iceseg_gt3r['atl06_quality_summary']\n",
    "\n",
    "\n",
    "gtl1_df['bsnow_conf'] = iceseg_gtl1['geophysical']['bsnow_conf']\n",
    "gtr1_df['bsnow_conf'] = iceseg_gt1r['geophysical']['bsnow_conf']\n",
    "gtl2_df['bsnow_conf'] = iceseg_gt21['geophysical']['bsnow_conf']\n",
    "gtr2_df['bsnow_conf'] = iceseg_gt2r['geophysical']['bsnow_conf']\n",
    "gtl3_df['bsnow_conf'] = iceseg_gt31['geophysical']['bsnow_conf']\n",
    "gtr3_df['bsnow_conf'] = iceseg_gt3r['geophysical']['bsnow_conf']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [],
   "source": [
    "beam1_df = pd.concat([gtl1_df, gtr1_df], axis=1)\n",
    "beam2_df = pd.concat([gtl2_df, gtr2_df], axis=1)\n",
    "beam3_df = pd.concat([gtl3_df, gtr3_df], axis=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [],
   "source": [
    "gtl1_df = gtl1_df[gtl1_df['atl06_quality_summary'] == 0]\n",
    "gtr1_df = gtr1_df[gtr1_df['atl06_quality_summary'] == 0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "BokehUserWarning: ColumnDataSource's columns must be of the same length. Current lengths: ('x', 5755), ('y', 2461)\n",
      "BokehUserWarning: ColumnDataSource's columns must be of the same length. Current lengths: ('x', 5755), ('y', 2164)\n",
      "BokehUserWarning: ColumnDataSource's columns must be of the same length. Current lengths: ('x', 6662), ('y', 2461)\n",
      "BokehUserWarning: ColumnDataSource's columns must be of the same length. Current lengths: ('x', 6663), ('y', 2164)\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "'/home/nrhagen/Documents/sathackweek/project/WaterNoice/notebooks/megadunes.html'"
      ]
     },
     "execution_count": 52,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "gtl1_df= gtl1_df[gtl1_df['x_atc'] < 28975107]\n",
    "gtr1_df = gtr1_df[gtr1_df['x_atc'] < 28975107]\n",
    "# # create a new plot with a title and axis labels\n",
    "y_min, y_max =(np.min(gtl1_df['h_li'])-2),(np.max(gtl1_df['h_li'])+2)\n",
    "\n",
    "\n",
    "p = figure(title=\"atl06\", x_axis_label='x_atc', y_axis_label='h_li',y_range=(y_min,y_max),sizing_mode='stretch_both')\n",
    "\n",
    "\n",
    "color_list = ['lightcoral','crimson','cornflowerblue','royalblue','lightgreen','seagreen']\n",
    "\n",
    "\n",
    "\"\"\" add shaded areas where blowing snow == 'high'\"\"\"\n",
    "\n",
    "p.vbar(x=gtr1_df['x_atc'][gtr1_df['bsnow_conf']==6], width=5, bottom=0,\n",
    "       top=y_max, color=\"coral\", alpha=0.5,legend='bsnow_6_gtr1')\n",
    "  \n",
    "       \n",
    "p.scatter(gtl1_df['x_atc'], gtl1_df['h_li'],color = color_list[0], size=10, fill_alpha=0.7,legend='gtl1')\n",
    "p.scatter(gtr1_df['x_atc'], gtr1_df['h_li'], color = color_list[1],size=10, fill_alpha=0.5,legend='gtr1')\n",
    "p.scatter(gtl2_df['x_atc'], gtl1_df['h_li'], color = color_list[2],size=10, fill_alpha=0.7,legend='gtl2')\n",
    "p.scatter(gtr2_df['x_atc'], gtr1_df['h_li'], color = color_list[3],size=10, fill_alpha=0.5,legend='gtr2')\n",
    "p.scatter(gtl3_df['x_atc'], gtl1_df['h_li'], color = color_list[4],size=10, fill_alpha=0.7,legend='gtl3')\n",
    "p.scatter(gtr3_df['x_atc'], gtr1_df['h_li'], color = color_list[5],size=10, fill_alpha=0.5,legend='gtr3')\n",
    "# show the results\n",
    "\n",
    "# p.add_tools(HoverTool(tooltips=None, renderers=[gtl1], mode='hline'))    \n",
    "\n",
    "# p.legend.location = \"top_left\"\n",
    "p.legend.click_policy=\"hide\"\n",
    "show(p)\n",
    "save(p, 'megadunes.html')\n",
    "\n",
    "\n",
    "# from bokeh.plotting import figure, output_file, save\n",
    "# p = figure(title=\"Basic Title\", plot_width=300, plot_height=300)\n",
    "# p.circle([1, 2], [3, 4])\n",
    "# output_file(\"test.html\")\n",
    "# save(p)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from mpl_toolkits.mplot3d import Axes3D\n",
    "f = plt.figure()\n",
    "ax3D = f.add_subplot(111, projection='3d')\n",
    "# ax3D.scatter(gtl1_df['lon'], gtl1_df['lat'], gtl1_df['h_li'], c=gtl1_df['bsnow_conf'], s=1, cmap='inferno')\n",
    "ax3D.scatter(gtr1_df['lon'], gtr1_df['lat'], gtr1_df['h_li'], c=gtr1_df['bsnow_conf'], s=1, cmap='inferno')\n",
    "# gtl1_df = ax3D.scatter(gtl1_df['lon'], gtl1_df['lat'], gtl1_df['h_li'], c=gtl1_df['bsnow_conf'], s=1, cmap='inferno')\n",
    "# gtl1_df = ax3D.scatter(gtl1_df['lon'], gtl1_df['lat'], gtl1_df['h_li'], c=gtl1_df['bsnow_conf'], s=1, cmap='inferno')\n",
    "\n",
    "#sc = ax3D.scatter(glas_df['lon'], glas_df['lat'], glas_df['glas_z'], c=glas_df['decyear'], s=1, cmap='inferno')\n",
    "plt.colorbar(sc)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "gtl1_df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "# import fiona\n",
    "\n",
    "# fiona.supported_drivers['KML'] = 'rw'\n",
    "\n",
    "# gtl1_gdf = geopandas.GeoDataFrame(\n",
    "#     gtl1_df, geometry=geopandas.points_from_xy(gtl1_df.lon, gtl1_df.lat))\n",
    "\n",
    "# # gtl1_gdf.to_file(\"gtl1_gdf_dunes.gpkg\", driver=\"GPKG\")\n",
    "# gtl1_gdf.to_file('gtl1_gdf_dunes.kml', driver='KML')\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "\"\"\"Blowing snow confidence. -3=surface not detected; -2=no\n",
    "surface wind;-1=no scattering layer found; 0=no top layer\n",
    "found; 1=none­little; 2=weak; 3=moderate; 4=moderate­high;\n",
    "5=high; 6=very high\n",
    "flag_values: -3, -2, -1, 0, 1, 2, 3, 4, 5, 6\n",
    "flag_meanings : surface_not_detected no_surface_wind\n",
    "no_scattering_layer_found no_top_layer_found none_little\n",
    "weak moderate moderate_high high very_high\n",
    "\"\"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "gtl1_df['bsnow_conf'].value_counts()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "gtl1_df['lon'][0],gtl1_df['lat'][0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
